package com.qx.controller;


import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.qx.po.Employees;
import com.qx.po.Supplier;
import com.qx.service.IEmployeesService;


@Controller
public class EmployeesController {
    @Autowired
    public IEmployeesService iemployeesservice;
    
    @RequestMapping("/toemployeeslogin")
    public String employeeslogin() throws Exception{
        return "/employeeslogin";
    }
    @RequestMapping("/tomanagelogin")
    public String tomanagelogin() throws Exception{
        return "/managelogin";
    }
   
    @RequestMapping("/employeeslogin")
    public String login(@RequestParam("eno") String eno, @RequestParam("epassword")String epassword, HttpSession session) throws Exception{
        Employees employees = new Employees();
        employees.setEno(eno);
        employees.setEpassword(epassword);
        Employees employees1 = iemployeesservice.selectEmployeesByenoAndepassword(employees);
        if(employees1 != null && ((employees1.getElevel()).equals("02"))){
        	session.setAttribute("employees", employees1);
        	return "/employeeschoose";
        }
        else{
            //如果不存在，则跳转到 login.jsp页面重新登录 	
        	 return "/employeeslogin";
        }
    }
    @RequestMapping("/managelogin")
    public String manageloginlogin(@RequestParam("eno") String eno, @RequestParam("epassword")String epassword, HttpSession session) throws Exception{
        Employees employees = new Employees();
        employees.setEno(eno);
        employees.setEpassword(epassword);
        Employees employees1 = iemployeesservice.selectEmployeesByenoAndepassword(employees);
        if(employees1 != null && ((employees1.getElevel()).equals("01"))){
        	session.setAttribute("employees", employees1);
        	return "/managechoose";
        }
        else{
            //如果不存在，则跳转到 login.jsp页面重新登录 	
        	 return "/managelogin";
        }
    }
    
    @RequestMapping("touserinformation")
    public String userinformation(Model model, HttpSession session) throws Exception { 
    	Employees employee = (Employees)session.getAttribute("employees");
    	model.addAttribute("employees", employee);
        return "/userinformation";
    }
    
    @RequestMapping("touserinformation1")
    public String userinformation1(Model model, HttpSession session) throws Exception { 
    	Employees employee = (Employees)session.getAttribute("employees");
    	model.addAttribute("employees", employee);
        return "/userinformation1";
    }
    
    @RequestMapping("toemployeesall")
	 public String toemployeesall(Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 List<Employees> employeeslist = iemployeesservice.selectEmployeesAll();
		 model.addAttribute("employeeslist", employeeslist);
		 model.addAttribute("employees", employee);
		 return "/employeesinformation";
		    }
    
    @RequestMapping("toaddemployees")
	 public String toaddemployees(Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 model.addAttribute("employees", employee);
		 return "/addemployees";
	 		}
    
    @RequestMapping("addemployees")
	 public String addemployees(@RequestParam("eno") String eno, @RequestParam("ename") String ename, @RequestParam("epassword") String epassword,
			 @RequestParam("elevel") String elevel, @RequestParam("etel") String etel, @RequestParam("esalary") int esalary,
			 @RequestParam("etips") String etips,Model model, HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Employees employees = new Employees();
		 employees.setEno(eno);
		 employees.setEname(ename);
		 employees.setEpassword(epassword);
		 employees.setElevel(elevel);
		 employees.setEtel(etel);
		 employees.setEsalary(esalary);
		 employees.setEtips(etips);
		 iemployeesservice.insertEmployees(employees);
		 List<Employees> employeeslist = iemployeesservice.selectEmployeesAll();
		 model.addAttribute("employeeslist", employeeslist);
		 model.addAttribute("employees", employee);
		 return "/employeesinformation";
	 		}
    
    @RequestMapping("deleteemployees/{eno}")
	 public String deleteemployees(Model model, HttpSession session, @PathVariable("eno") String eno) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 iemployeesservice.deleteEmployeesByeno(eno);
		 List<Employees> employeeslist = iemployeesservice.selectEmployeesAll();
		 model.addAttribute("employeeslist", employeeslist);
		 model.addAttribute("employees", employee);
		 return "/employeesinformation";
	 		}
    
    @RequestMapping("toupdateemployees/{eno}")
	 public String toupdateemployees(HttpServletResponse response,Model model, HttpSession session, @PathVariable("eno") String eno) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Employees employees = iemployeesservice.selectEmployeesByeno(eno);
		 model.addAttribute("employee", employees);
		 model.addAttribute("employees", employee);
		 return "/updateemployees";
	 		}

	 @RequestMapping("toemployeesinformation1/{eno}")
	 public String toemployeesinformation1(Model model, @PathVariable("eno") String eno, @RequestParam("ename") String ename, 
			 @RequestParam("epassword") String epassword, @RequestParam("elevel") String elevel, @RequestParam("etel") String etel, 
			 @RequestParam("esalary") int esalary, @RequestParam("etips") String etips,HttpSession session) throws Exception {
		 Employees employee = (Employees)session.getAttribute("employees");
		 Employees employees = new Employees();
		 employees.setEno(eno);
		 employees.setEname(ename);
		 employees.setEpassword(epassword);
		 employees.setElevel(elevel);
		 employees.setEtel(etel);
		 employees.setEsalary(esalary);
		 employees.setEtips(etips);
		 iemployeesservice.updateEmployeesByeno(employees);
		 List<Employees> employeeslist = iemployeesservice.selectEmployeesAll();
		 model.addAttribute("employeeslist", employeeslist);
		 model.addAttribute("employees", employee);
		 return "/employeesinformation";
	 		}


 
}